import { formatParams } from "@/utils/index";
import request from "@/utils/request";

/**
 * 获取云存储临时密钥
 */
export function stsDispatcherAPI() {
    return request({
        url: "/x/v1/oss/stsDispatcher",
        method: "GET",
    });
}
/**
 * 排序音频资源接口
 * @param {Object} sortRule 排序规则
 */
export function sortAudioResourceAPI(sortRule) {
    return request({
        url: "/oss/sort/audio",
        method: "PUT",
        data: { sortRule },
    });
}
/**
 * 删除音频资源接口
 * @param {number} rid 音频资源id
 */
export function deleteAudioReourceAPI(rid) {
    return request({
        url: `/oss/delete/audio/${rid}`,
        method: "DELETE",
    });
}
/**
 *  修改音频资源信息接口
 * @param {number} rid 音频资源id
 * @param {Object} resource 音频资源
 */
export function updateAudioResourceAPI(rid, resource) {
    const column = ["cover", "name", "artist", "mark", "lyric"];
    const paramData = formatParams(resource, column);
    return request({
        url: `/oss/update/audio/${rid}`,
        method: "PUT",
        data: paramData,
    });
}
/**
 *服务器端简单上传接口
 * @param {File} file 需上传的文件
 * @param {string} dir 上传的文件夹
 */
export function serverSimpleUploaderAPI(file, dir = "") {
    const formData = new FormData();
    formData.append("file", file);
    formData.append("dir", dir);
    return request({
        url: "/oss/uploader",
        method: "POST",
        data: formData,
    });
}
/**
 * 删除资源表的资源
 * @param {number} rid 资源id
 */
export function deleteResourceAPI(rid) {
    return request({
        url: `/oss/resource/${rid}`,
        method: "DELETE",
    });
}
/**
 * 站点配置图片上传接口
 * @param {File} file 站点配置图片文件
 */
export function clientHeaderUploaderAPI(file, action) {
    const formData = new FormData();
    formData.append("file", file);
    formData.append("action", action);
    return request({
        url: "/oss/uploader/chilentOption",
        method: "POST",
        data: formData,
    });
}
/**
 * 设置配置接口
 * @param {string} name 配置名
 * @param {string} value 配置内容
 * @param {string} group 配置组
 */
export function setOptionAPI(name, value, group) {
    return request({
        url: `/options/set/${name}`,
        method: "POST",
        data: { value, group },
    });
}
/**
 * 获取配置接口
 * @param {string} group 配置组
 */
export function getOptionAPI(group) {
    return request({
        url: `/options/get/${group}`,
        method: "GET",
    });
}

// 获取验证码
export function getVerifyCode({ type, phone, email }) {
    if (!type) {
        return console.error("没有设置验证码类型");
    }
    const baseUrl = type === "phone" ? "/verifycodeSMS" : "/verifycodeEmail";
    return request({
        url: baseUrl,
        method: "post",
        data: { phone, email },
    });
}
// 验证手机号或邮箱是否已被使用
export function verifySignCanUse({ type, phone, email }) {
    if (!type) {
        return console.error("没有设置类型");
    }
    const baseUrl = type === "phone" ? "/testPhone" : "/testEmail";
    return request({
        url: baseUrl,
        method: "post",
        data: { phone, email },
    });
}
